package com.summer.service.user;

import com.summer.dao.BaseDao;
import com.summer.dao.user.UserDao;
import com.summer.dao.user.UserDaoImpl;
import com.summer.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者:夏天
 * qq:1205232048
 */
public class UserServiceImpl implements UserService {

    //service调用dao层
    private UserDao userDao;

    //new一个service类就会调用对应的dao类
    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    /**
     * 根据用户id 返回删除结果
     * @param delId
     * @return
     */
    public boolean deleteUserById(Integer delId) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (userDao.deleteUserById(connection, delId) > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    /**
     * 根据用户Id 返回用户
     * @param id
     * @return
     */
    public User getUserById(String id) {
        // TODO Auto-generated method stub
        User user = null;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserById(connection, id);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            user = null;
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    /**
     * 根据用户 返回修改结果
     * @param user
     * @return
     */
    public boolean modifyUser(User user) {
        // TODO Auto-generated method stub
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (userDao.modifyUser(connection, user) > 0) {
                flag = true;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    /**
     * 根据用户编码 前端ajax异步请求得到用户 用来给servlet层做交互
     * @param userCode
     * @return
     */
    public User selectUserCodeExist(String userCode) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    /**
     * 根据 用户编码 用户密码 返回用户
     * @param userCode
     * @param password
     * @return User
     */
    public User login(String userCode, String password){
        Connection connection = null;
        User user = null;


        try {
            connection = BaseDao.getConnection();
            //通过业务层来调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, userCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        //匹配密码
        if(user != null){
            if(!user.getUserPassword().equals(password)){
                return null;
            }
        }
        return user;
    }

    /**
     * 根据用户id 用户新密码 返回修改密码结果
     * @param id
     * @param newpassword
     * @return boolean
     */
    public boolean modifyPasswordById(int id, String newpassword) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if(userDao.modifyPasswordById(connection,id,newpassword) > 0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    /**
     * 根据用户姓名 用户角色 返回用户数量
     * @param userName
     * @param userRole
     * @return int
     */
    public int getUserCount(String userName, int userRole) {
        Connection connection = null;
        int userCount = 0;
        try {
            connection = BaseDao.getConnection();
            userCount = userDao.getUserCount(connection, userName, userRole);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return userCount;
    }

    /**
     * 根据用户姓名,用户角色,当前页数,页面大小--->返回用户列表
     * @param userName
     * @param userRole
     * @param currentPageNo
     * @param pageSize
     * @return List<User>
     */
    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> users = new ArrayList<User>();
        try {
            connection = BaseDao.getConnection();
            users = userDao.getUserList(connection, userName, userRole, currentPageNo, pageSize);
            System.out.println("UserServiceImpl->getUserList->users = " + users);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("UserServiceImpl->userRole = " + userRole);
        return users;
    }

    /**
     *根据用户 返回添加结果
     * @param user
     * @return boolean
     */
    public boolean addUser(User user) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//关闭自动提交事务等价于开启JDBC事务
            int i = userDao.addUser(connection, user);
            connection.commit();
            if(i > 0){
                flag =true;
                System.out.println("用户添加成功");
            }else {
                System.out.println("用户添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();//有异常 回滚
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                BaseDao.closeResource(connection,null,null);
            }
        }
        return false;
    }


}
